#include <iostream>
#include <unistd.h>
#include <csignal>
#include <sys/wait.h>

int flag = 0;
void TestSIGCHLD(int signum)
{
	// std::cout << "子进程退出，测试SIGHLD信号，接收到的信号为：" << signum << std::endl;
	// flag = 1;
	while (true)
	{
		pid_t n = waitpid(-1, nullptr, WNOHANG);
		if (n == 0)
		{
			std::cout << "没有需要回收的进程" << std::endl;
			break;
		}
		else if (n < 0)
		{
			std::cout << "waitpid error" << std::endl;
			break;
		}
		else{
			std::cout<<"成功回收一个子进程："<<std::endl;
		}
	}

	//std::cout<<"所有进程已完成回收"<<std::endl;
}

int main()
{
	//signal(SIGCHLD, TestSIGCHLD);
	signal(SIGCHLD, SIG_IGN);
	for (int i = 0; i < 10; i++)
	{
		pid_t n = fork();
		if (n == 0)
		{	
			if(i < 6){
				std::cout << "child process" << std::endl;
				sleep(3);
				exit(0);
			}
			else
				pause();
		}
	}

	while (true)
	{
		pause();
		// if (flag)
		// 	break;
	}
	std::cout << "parent process exit" << std::endl;

	return 0;
}